<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/yuwanzi.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/yuwanzi.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/yuwanzi.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/yuwanzi.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/yuwanzi.io/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"suyuhuan.gitee.io","root":"/yuwanzi.io/","images":"/yuwanzi.io/images","scheme":"Muse","version":"8.2.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Suche...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="概述 &amp;nbsp;&amp;nbsp;Spring Batch是一个轻量级的处理大量数据操作的框架,主要用于读取大量数据,然后进行一定处理后输出成指定的格式. &amp;nbsp;&amp;nbsp;Spring Batch 提供了大量可重用的组件,包括了日志、追踪、事务、任务作业统计、任务重启、跳过、重复、资源管理。对于大数据量和高性能的批处理任务,Spring Batch 同样提供了高级功能和特性来支持,比如分区功能">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Batch数据批处理">
<meta property="og:url" content="https://suyuhuan.gitee.io/yuwanzi.io/2016/08/04/2016-08-4-Spring-batch/index.html">
<meta property="og:site_name" content="玉丸子 | Blog">
<meta property="og:description" content="概述 &amp;nbsp;&amp;nbsp;Spring Batch是一个轻量级的处理大量数据操作的框架,主要用于读取大量数据,然后进行一定处理后输出成指定的格式. &amp;nbsp;&amp;nbsp;Spring Batch 提供了大量可重用的组件,包括了日志、追踪、事务、任务作业统计、任务重启、跳过、重复、资源管理。对于大数据量和高性能的批处理任务,Spring Batch 同样提供了高级功能和特性来支持,比如分区功能">
<meta property="og:locale">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6navcbgzhj20ki0860tg.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/63503acbjw1f6ncme1ztdj20kk0e0n09.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/63503acbjw1f6ncmeoja5j20c4054wey.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6ncmeln1gj20he07tjsq.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6nd7a60nrj20kv06adh4.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/63503acbjw1f6ndeuabtvj20gt06ijs8.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6ndevin2gj20p30ku44i.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6ndeuf8dkj20kc0c8acs.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6ndev1nz8j20dk04s0t9.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6ndeupgvxj20g407lgmy.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6nejvlqcuj20hs0b3jtx.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6nejv24v2j20u70e1n1d.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6neue2xagj20lw0i3n25.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6s3o0w6n1j20u60dtdjx.jpg">
<meta property="article:published_time" content="2016-08-04T10:00:00.000Z">
<meta property="article:modified_time" content="2020-11-07T00:58:17.000Z">
<meta property="article:author" content="玉丸子">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Spring">
<meta property="article:tag" content="Spring Boot">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6navcbgzhj20ki0860tg.jpg">


<link rel="canonical" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/08/04/2016-08-4-Spring-batch/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh'
  };
</script>
<title>Spring Batch数据批处理 | 玉丸子 | Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/yuwanzi.io/atom.xml" title="玉丸子 | Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Navigationsleiste an/ausschalten" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/yuwanzi.io/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">玉丸子 | Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Inhaltsverzeichnis
        </li>
        <li class="sidebar-nav-overview">
          Übersicht
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A6%82%E8%BF%B0"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="nav-number">2.</span> <span class="nav-text">应用场景</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%84%E6%88%90%E7%BB%93%E6%9E%84"><span class="nav-number">3.</span> <span class="nav-text">组成结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Job-Listener"><span class="nav-number">4.</span> <span class="nav-text">Job Listener</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86%E5%92%8C%E6%A0%A1%E9%AA%8C"><span class="nav-number">5.</span> <span class="nav-text">数据处理和校验</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86"><span class="nav-number">5.1.</span> <span class="nav-text">数据处理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E6%A0%A1%E9%AA%8C"><span class="nav-number">5.2.</span> <span class="nav-text">数据校验</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%82%E6%95%B0%E5%90%8E%E7%BD%AE%E7%BB%91%E5%AE%9A"><span class="nav-number">6.</span> <span class="nav-text">参数后置绑定</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Boot%E6%94%AF%E6%8C%81"><span class="nav-number">7.</span> <span class="nav-text">Spring Boot支持</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Config-Example"><span class="nav-number">7.1.</span> <span class="nav-text">Config Example</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#end"><span class="nav-number">8.</span> <span class="nav-text">end</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">玉丸子</p>
  <div class="site-description" itemprop="description">这里是玉丸子的个人博客,与你一起发现更大的世界。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/yuwanzi.io/archives">
          <span class="site-state-item-count">68</span>
          <span class="site-state-item-name">Artikel</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/yuwanzi.io/categories/">
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">Kategorien</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/yuwanzi.io/tags/">
        <span class="site-state-item-count">46</span>
        <span class="site-state-item-name">schlagwörter</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh">
    <link itemprop="mainEntityOfPage" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/08/04/2016-08-4-Spring-batch/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/yuwanzi.io/images/avatar.gif">
      <meta itemprop="name" content="玉丸子">
      <meta itemprop="description" content="这里是玉丸子的个人博客,与你一起发现更大的世界。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="玉丸子 | Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Spring Batch数据批处理
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Veröffentlicht am</span>

      <time title="Erstellt: 2016-08-04 18:00:00" itemprop="dateCreated datePublished" datetime="2016-08-04T18:00:00+08:00">2016-08-04</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Bearbeitet am</span>
        <time title="Geändert am: 2020-11-07 08:58:17" itemprop="dateModified" datetime="2020-11-07T08:58:17+08:00">2020-11-07</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">in</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">后端</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/Spring-Boot/" itemprop="url" rel="index"><span itemprop="name">Spring Boot</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><hr>
<p>&nbsp;&nbsp;Spring Batch是一个轻量级的处理大量数据操作的框架,主要用于读取大量数据,然后进行一定处理后输出成指定的格式.</p>
<p>&nbsp;&nbsp;Spring Batch 提供了大量可重用的组件,包括了日志、追踪、事务、任务作业统计、任务重启、跳过、重复、资源管理。对于大数据量和高性能的批处理任务,Spring Batch 同样提供了高级功能和特性来支持,比如分区功能、远程功能。总之,通过 Spring Batch 能够支持简单的、复杂的和大数据量的批处理作业.</p>
<p>&nbsp;&nbsp;Spring Batch 是一个批处理应用框架,不是调度框架,但需要和调度框架合作来构建完成的批处理任务。它只关注批处理任务相关的问题,如事务、并发、监控、执行等,并不ﰁ供相应的调度功能。如果需要使用调用框架,在商业软件和开源软件中已经有很多优秀的企业级调度框架(如 Quartz、Tivoli、Control-M、Cron 等)可以使用.</p>
<h3 id="应用场景"><a href="#应用场景" class="headerlink" title="应用场景"></a>应用场景</h3><hr>
<ul>
<li>周期性的提交处理.</li>
<li>并行处理任务.</li>
<li>消息驱动应用分级处理.</li>
<li>手工或调度使任务失败之后重新启动.</li>
<li>有依赖步骤的顺序执行(使用工作流驱动扩展).</li>
<li>处理时跳过部分记录.</li>
<li>成批事务：为小批量的或有的存储过程/脚本的场景使用.</li>
</ul>
<h3 id="组成结构"><a href="#组成结构" class="headerlink" title="组成结构"></a>组成结构</h3><hr>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6navcbgzhj20ki0860tg.jpg"></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody><tr>
<td>Job</td>
<td>实际要执行的任务,包含一个或多个Step</td>
</tr>
<tr>
<td>JobRepository</td>
<td>用于注册Job的容器</td>
</tr>
<tr>
<td>JobLauncher</td>
<td>用于启动Job的接口</td>
</tr>
<tr>
<td>Step</td>
<td>Step步骤包含ItemReader,ItemProcessor,ItemWriter</td>
</tr>
<tr>
<td>ItemReader</td>
<td>用于读取数据的接口</td>
</tr>
<tr>
<td>ItemProcessor</td>
<td>用于处理数据的接口</td>
</tr>
<tr>
<td>ItemWriter</td>
<td>用于输出数据的接口</td>
</tr>
</tbody></table>
<p><strong>&nbsp;&nbsp;以上组件只需要注册成Spring的Bean即可,并在配置类上使用<code>@EnableBatchProcessing</code>开启批处理的支持.</strong></p>
<h3 id="Job-Listener"><a href="#Job-Listener" class="headerlink" title="Job Listener"></a>Job Listener</h3><hr>
<p>&nbsp;&nbsp;如果想要监听Job的执行情况,则需要定义一个实现了<strong>JobExecutionListener</strong>的类,并在定义Job的Bean上绑定该监听器.</p>
<p><img src="http://ww1.sinaimg.cn/mw690/63503acbjw1f6ncme1ztdj20kk0e0n09.jpg"></p>
<p><img src="http://ww1.sinaimg.cn/mw690/63503acbjw1f6ncmeoja5j20c4054wey.jpg"></p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6ncmeln1gj20he07tjsq.jpg"></p>
<h3 id="数据处理和校验"><a href="#数据处理和校验" class="headerlink" title="数据处理和校验"></a>数据处理和校验</h3><hr>
<p>&nbsp;&nbsp;数据的处理和校验都要通过ItemProcessor接口实现来完成的.</p>
<h4 id="数据处理"><a href="#数据处理" class="headerlink" title="数据处理"></a>数据处理</h4><hr>
<p>&nbsp;&nbsp;实现ItemProcessor接口,并重写process方法,即可对数据进行处理.输入参数是从ItemReader读取到的数据,返回给ItemWriter.</p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6nd7a60nrj20kv06adh4.jpg"></p>
<h4 id="数据校验"><a href="#数据校验" class="headerlink" title="数据校验"></a>数据校验</h4><hr>
<p>&nbsp;&nbsp;数据校验可以使用JSR-303的注解来校验ItemReader读取的数据是否符合要求.</p>
<p><img src="http://ww1.sinaimg.cn/mw690/63503acbjw1f6ndeuabtvj20gt06ijs8.jpg" alt="定义实体类的检验规则"></p>
<p><img src="http://ww4.sinaimg.cn/mw690/63503acbjw1f6ndevin2gj20p30ku44i.jpg" alt="定义校验器"></p>
<p><img src="http://ww4.sinaimg.cn/mw690/63503acbjw1f6ndeuf8dkj20kc0c8acs.jpg" alt="ItemProcessor"></p>
<p><img src="http://ww4.sinaimg.cn/mw690/63503acbjw1f6ndev1nz8j20dk04s0t9.jpg" alt="注册校验器"></p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6ndeupgvxj20g407lgmy.jpg" alt="注册ItemProcessor"></p>
<h3 id="参数后置绑定"><a href="#参数后置绑定" class="headerlink" title="参数后置绑定"></a>参数后置绑定</h3><hr>
<p>&nbsp;&nbsp;实现参数后置绑定可以在JobParameters中绑定参数,在Bean定义的时候使用一个特殊的Bean生命周期注解<code>@StepScope</code>,然后通过<code>@Value</code>注入此参数.</p>
<p><img src="http://ww4.sinaimg.cn/mw690/63503acbjw1f6nejvlqcuj20hs0b3jtx.jpg" alt="绑定参数"></p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6nejv24v2j20u70e1n1d.jpg" alt="定义ItemReader"></p>
<h3 id="Spring-Boot支持"><a href="#Spring-Boot支持" class="headerlink" title="Spring Boot支持"></a>Spring Boot支持</h3><hr>
<p>&nbsp;&nbsp;Spring Boot自动初始化了Spring Batch存储批处理的数据库,并当程序启动时,会自动执行Job.</p>
<p>&nbsp;&nbsp;Spring Boot使用以<code>spring.batch</code>为前缀的属性进行相关配置.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">spring.batch.job.name&#x3D;job1,job2 #启动时要执行的job,默认执行全部job.</span><br><span class="line">spring.batch.job.enabled&#x3D;true #是否自动执行定义的job,默认为true</span><br><span class="line">spring.batch.initializer.enabled&#x3D;true #是否初始化Spring Batch的数据库,默认为true.</span><br><span class="line">spring.batch.schema&#x3D;</span><br><span class="line">spring.batch.table-prefix&#x3D; #设置Spring Batch的数据库表的前缀.</span><br></pre></td></tr></table></figure>
<p>&nbsp;&nbsp;Spring Batch默认自动加载hsqldb驱动,如要使用其他数据库驱动则需要手动去除.</p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6neue2xagj20lw0i3n25.jpg"></p>
<h4 id="Config-Example"><a href="#Config-Example" class="headerlink" title="Config Example"></a>Config Example</h4><p><img src="http://ww2.sinaimg.cn/mw690/63503acbjw1f6s3o0w6n1j20u60dtdjx.jpg"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义ItemProcessor</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> ItemProcessor&lt;Person, Person&gt; <span class="title">processor</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 使用自定义的ItemProcessor</span></span><br><span class="line">    CsvItemProcessor processor = <span class="keyword">new</span> CsvItemProcessor();</span><br><span class="line">    <span class="comment">// 指定校验器</span></span><br><span class="line">    processor.setValidator(csvBeanValidator());</span><br><span class="line">    <span class="keyword">return</span> processor;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义Validator</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> Validator&lt;Person&gt; <span class="title">csvBeanValidator</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> CsvBeanValidator&lt;Person&gt;();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义ItemWriter</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> ItemWriter&lt;Person&gt; <span class="title">writer</span><span class="params">(DataSource dataSource)</span> </span>&#123;</span><br><span class="line">    JdbcBatchItemWriter&lt;Person&gt; writer = <span class="keyword">new</span> JdbcBatchItemWriter&lt;&gt;();</span><br><span class="line">    writer.setItemSqlParameterSourceProvider</span><br><span class="line">            (<span class="keyword">new</span> BeanPropertyItemSqlParameterSourceProvider&lt;Person&gt;());</span><br><span class="line">    <span class="comment">// 设置要执行批处理的sql语句</span></span><br><span class="line">    String sql = <span class="string">&quot;insert into person &quot;</span> + <span class="string">&quot;(id,name,age,nation,address)&quot;</span></span><br><span class="line">            + <span class="string">&quot;values(hibernate_sequence.nextval,:name,:age,:nation,:address)&quot;</span>;</span><br><span class="line">    writer.setSql(sql);</span><br><span class="line">    writer.setDataSource(dataSource);</span><br><span class="line">    <span class="keyword">return</span> writer;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义JobRepository</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> JobRepository <span class="title">jobRepository</span><span class="params">(DataSource dataSource,</span></span></span><br><span class="line"><span class="function"><span class="params">                                   PlatformTransactionManager transactionManager)</span></span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    JobRepositoryFactoryBean jobRepositoryFactoryBean = <span class="keyword">new</span> JobRepositoryFactoryBean();</span><br><span class="line">    jobRepositoryFactoryBean.setDataSource(dataSource);</span><br><span class="line">    jobRepositoryFactoryBean.setTransactionManager(transactionManager);</span><br><span class="line">    jobRepositoryFactoryBean.setDatabaseType(<span class="string">&quot;mysql&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> jobRepositoryFactoryBean.getObject();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义JobLauncher</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> JobLauncher <span class="title">jobLauncher</span><span class="params">(DataSource dataSource,</span></span></span><br><span class="line"><span class="function"><span class="params">                               PlatformTransactionManager transactionManager)</span></span></span><br><span class="line"><span class="function">        <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    SimpleJobLauncher jobLauncher = <span class="keyword">new</span> SimpleJobLauncher();</span><br><span class="line">    jobLauncher.setJobRepository(jobRepository(dataSource, transactionManager));</span><br><span class="line">    <span class="keyword">return</span> jobLauncher;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义Job</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> Job <span class="title">importJob</span><span class="params">(JobBuilderFactory jobs, Step s1)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> jobs.get(<span class="string">&quot;importJob&quot;</span>)</span><br><span class="line">            .incrementer(<span class="keyword">new</span> RunIdIncrementer())</span><br><span class="line">            .flow(s1) <span class="comment">// 为job指定step</span></span><br><span class="line">            .end()</span><br><span class="line">            .listener(csvJobListener()) <span class="comment">// 绑定监听器</span></span><br><span class="line">            .build();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义Step</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> Step <span class="title">step1</span><span class="params">(StepBuilderFactory stepBuilderFactory, ItemReader&lt;Person&gt; reader,</span></span></span><br><span class="line"><span class="function"><span class="params">                  ItemWriter&lt;Person&gt; writer,</span></span></span><br><span class="line"><span class="function"><span class="params">                  ItemProcessor&lt;Person, Person&gt; processor)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> stepBuilderFactory</span><br><span class="line">            .get(<span class="string">&quot;step1&quot;</span>)</span><br><span class="line">            .&lt;Person, Person&gt;chunk(<span class="number">65000</span>) <span class="comment">// 批处理每次提交65000条数据.</span></span><br><span class="line">            .reader(reader)</span><br><span class="line">            .processor(processor)</span><br><span class="line">            .writer(writer)</span><br><span class="line">            .build();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 定义自定义的Job监听器</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> CsvJobListener <span class="title">csvJobListener</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> CsvJobListener();</span><br><span class="line">&#125;</span><br><span class="line">	</span><br></pre></td></tr></table></figure>
<h3 id="end"><a href="#end" class="headerlink" title="end"></a>end</h3><blockquote>
<p>资料参考于 JavaEE开发的颠覆者: Spring Boot实战</p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/yuwanzi.io/tags/Java/" rel="tag"># Java</a>
              <a href="/yuwanzi.io/tags/Spring/" rel="tag"># Spring</a>
              <a href="/yuwanzi.io/tags/Spring-Boot/" rel="tag"># Spring Boot</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/08/03/2016-08-3-Spring-message/" rel="prev" title="Spring Boot消息通信">
                  <i class="fa fa-chevron-left"></i> Spring Boot消息通信
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/08/05/2016-08-5-Spring-monitor/" rel="next" title="Spring Boot 部署测试与应用监控">
                  Spring Boot 部署测试与应用监控 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">玉丸子</span>
</div>
  <div class="powered-by">Erstellt mit  <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/yuwanzi.io/js/utils.js"></script><script src="/yuwanzi.io/js/motion.js"></script><script src="/yuwanzi.io/js/schemes/muse.js"></script><script src="/yuwanzi.io/js/next-boot.js"></script>

  






  





</body>
</html>
